//
//  RegexSum.m
//  RegexDemo
//
//  Created by 小草毋正 on 2018/1/5.
//  Copyright © 2018年 C-LOVE. All rights reserved.
//

#import "RegexSumExercise.h"

@implementation RegexSumExercise


+ (void)test {
    
    
    //    [self exercise0Or1];
    //    [self exercise1OrMore];
    //    [self exercise0OrMore];
    //    [self exerciseTheD];
    //    [self exerciseTheS];
    //    [self exerciseTheW];
    //    [self exercisePoint];
    
    //    [self exerciseRepeatN];
    //    [self exerciseRepeatNOrMore];
    //    [self exerciseRepeatNToM];
    //    [self exerciseTheBigW];
    //    [self exerciseTheBigS];
    //    [self exerciseTheBigD];
    //    [self exerciseExecptX];
    //    [self is11Numbers];
    //    [self isCMPhoneNumber];
    [self isEmailAddress];
}



#pragma mark- ====---------------- 一些具体判断例子 Down ----------------====

#pragma mark- ==== 邮箱验证 ====
+(void)isEmailAddress {
    NSString *regex = @"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
    [self printerRegex:regex object:@"735296128@qq.com"];
    [self printerRegex:regex object:@"980@qq.com"];
    [self printerRegex:regex object:@"_sssc@168.com"];
    [self printerRegex:regex object:@"%scsa@128.com"];
    [self printerRegex:regex object:@"%scsa@128.comsdfkj"]; //不匹配
}


#pragma mark- ==== 移动号码 ====
/** 是否为11位数字 */
+ (void)isCMPhoneNumber {
    // 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
    NSString *regex = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d|705)\\d{7}$";
    [self printerRegex:regex object:@"18566699899"]; //不匹配
    [self printerRegex:regex object:@"13421232344"]; //匹配
    [self printerRegex:regex object:@"13509734352"]; //匹配
    [self printerRegex:regex object:@"13809734352"]; //匹配
    [self printerRegex:regex object:@"13309734323"]; //不匹配
    [self printerRegex:regex object:@"15109734323"]; //匹配
    [self printerRegex:regex object:@"15809734323"]; //匹配
    [self printerRegex:regex object:@"15209734323"]; //不匹配
    [self printerRegex:regex object:@"18209734353"]; //匹配
    [self printerRegex:regex object:@"18809734353"]; //匹配
    [self printerRegex:regex object:@"17059734353"]; //匹配
    [self printerRegex:regex object:@"15223"]; //不匹配
}

#pragma mark- ==== 匹配11个数字 ====
/** 是否为11位数字 */
+ (void)is11Numbers {
    /**
     * [0-9] 表示0到0这个范围
     * {11} 表示重复11次
     * 整体就是，11个0-9的数字
     */
    NSString *regex = @"^[0-9]{11}";
    [self printerRegex:regex object:@"18566699899"]; //匹配
    [self printerRegex:regex object:@"01234567890"]; //匹配
    [self printerRegex:regex object:@"2345"];        //不匹配
    [self printerRegex:regex object:@"sadfdsgf"];    //不匹配
}

#pragma mark- ====---------------- 一些具体判断例子 Up ----------------====


#pragma mark- ==== [^x] 匹配除了x以外的任意字符 ===
/** [^x] 匹配除了x以外的任意字符 */
+ (void)exerciseExecptX {
    NSString *regex = @"^[^x].*";
    [self printerRegex:regex object:@"02343"]; //匹配
    [self printerRegex:regex object:@"x4"];   //不匹配
    [self printerRegex:regex object:@"xxxx"]; //不匹配
    [self printerRegex:regex object:@"dfgh"]; //匹配
}

#pragma mark- ==== {n,m} 重复n到m次 ===
/** {n,m} 重复n到m次 */
+ (void)exerciseRepeatNToM {
    /** 开始2到5个0 */
    NSString *regex = @"^0{2,5}1.*";
    [self printerRegex:regex object:@"0000001"]; //不匹配
    [self printerRegex:regex object:@"00001213"];//匹配
    [self printerRegex:regex object:@"0012312"]; //匹配
    [self printerRegex:regex object:@"000001232"];//匹配
}

#pragma mark- ==== {n,} 重复n次或更多次 ===
/** {n,} 重复n次或更多次 */
+ (void)exerciseRepeatNOrMore {
    /** 开始5个0或更多 */
    NSString *regex = @"^0{5,}1.*";
    [self printerRegex:regex object:@"0000001"]; //匹配
    [self printerRegex:regex object:@"00001213"];//不匹配
    [self printerRegex:regex object:@"0012312"]; //不匹配
    [self printerRegex:regex object:@"000001232"];//匹配
}

#pragma mark- ==== {n} 重复n次 ===
/** {n} 重复n次 */
+ (void)exerciseRepeatN {
    /** 开始5个0 */
    NSString *regex = @"^0{5}1.*";
    [self printerRegex:regex object:@"0000001"]; //不匹配
    [self printerRegex:regex object:@"0000213"];  //不匹配
    [self printerRegex:regex object:@"002312"]; //不匹配
    [self printerRegex:regex object:@"000001232"]; //匹配
}

#pragma mark- ==== ? 重复零次或一次 ===
/** ? 重复零次或一次 */
+ (void)exercise0Or1 {
    /** a后面跟着0个或1个a */
    NSString *regex = @"^aa?b.*";
    [self printerRegex:regex object:@"aaaaa"]; //不匹配
    [self printerRegex:regex object:@"abbb"];  //匹配
    [self printerRegex:regex object:@"aabbb"]; //匹配
    [self printerRegex:regex object:@"aaabb"]; //不匹配
}

#pragma mark- ==== + 重复1次或更多次 ===
/** + 重复1次或更多次 */
+ (void)exercise1OrMore {
    /** a后面跟着1个或多个a */
    NSString *regex = @"^aa+b.*";
    [self printerRegex:regex object:@"aaaaa"]; //不匹配
    [self printerRegex:regex object:@"abbb"];  //不匹配
    [self printerRegex:regex object:@"bbb"];    //不匹配
    [self printerRegex:regex object:@"aaaabb"];  //匹配
}

#pragma mark- ==== * 重复零次或更多次 ===
/** * 重复零次或更多次 */
+ (void)exercise0OrMore {
    /** a后面跟着0个或多个a */
    NSString *regex = @"^aa*b.*";
    [self printerRegex:regex object:@"aaaaaaaa"]; //不匹配
    [self printerRegex:regex object:@"abbb"];     //匹配
    [self printerRegex:regex object:@"bbb"];      //不匹配
    [self printerRegex:regex object:@"aaaabb"];  //匹配
}

/**
 * \\b 匹配单词的开始或结束
 * ^   匹配字符串的开始
 * $   匹配字符串的结束
 * +   重复一次或更多次
 */

#pragma mark- ==== \\D 匹配任意非数字的字符 ====

/** \\D 匹配任意非数字的字符 */
+ (void)exerciseTheBigD {
    NSString *regex = @"^\\D.*";
    [self printerRegex:regex object:@"2asasd"]; //不匹配
    [self printerRegex:regex object:@"周小草"];  //匹配
    [self printerRegex:regex object:@"dsfd"];   //匹配
}


#pragma mark- ==== \\d 匹配数字 ====

/** \\d 匹配数字 */
+ (void)exerciseTheD {
    /** 数字开头 */
    NSString *regex = @"^\\d.*";
    [self printerRegex:regex object:@"2asasd"]; //匹配
    [self printerRegex:regex object:@"周小草"];  //不匹配
    [self printerRegex:regex object:@"dsfd"];  //不匹配
}


#pragma mark- ==== \\S 匹配任意不是空白符的字符 ====

/** \\S  匹配任意不是空白符的字符 */
+ (void)exerciseTheBigS {
    NSString *regex = @"^\\S.*";
    [self printerRegex:regex object:@" asasd"]; //不匹配
    [self printerRegex:regex object:@"周小草"];  //匹配
}


#pragma mark- ==== \\s 匹配任意的空白符 ====

/** \\s  匹配任意的空白符 */
+ (void)exerciseTheS {
    NSString *regex = @"^\\s.*";
    [self printerRegex:regex object:@" asasd"]; //bu匹配
    [self printerRegex:regex object:@"周小草"];  //不匹配
}

#pragma mark- ==== \\W 匹配字母或数字或下划线或汉字 ====
/** \\W 非匹配字母或数字或下划线或汉字 */
+ (void)exerciseTheBigW {
    NSString *regex = @"^\\W.*";
    [self printerRegex:regex object:@"asasd"];  //不匹配
    [self printerRegex:regex object:@"周小草"];  //不匹配
    [self printerRegex:regex object:@"12334"];  //不匹配
    [self printerRegex:regex object:@"*&*#^^"]; //匹配
    [self printerRegex:regex object:@"__isdhf"]; //不匹配
}

#pragma mark- ==== \\w 匹配字母或数字或下划线或汉字 ====

/** \\w 匹配字母或数字或下划线或汉字 */
+ (void)exerciseTheW {
    /** 开头为字母或数字或下划线或汉字 */
    NSString *regex = @"^\\w.*";
    [self printerRegex:regex object:@"asasd"];  //匹配
    [self printerRegex:regex object:@"周小草"];  //匹配
    [self printerRegex:regex object:@"12334"];  //匹配
    [self printerRegex:regex object:@"*&*#^^"]; //不匹配
    [self printerRegex:regex object:@"__isdhf"]; //匹配
}

#pragma mark- ==== . 匹配除了换行符一位的任意字符 ====

/** .匹配除了换行符一位的任意字符 */
+ (void)exercisePoint {
    NSString *regex;
    /** 前三个为任意字符 后面接着是abc */
    regex = @"^...abc";
    [self printerRegex:regex object:@"abcabc"];    //匹配
    [self printerRegex:regex object:@"周小草abc"];  //匹配
    [self printerRegex:regex object:@"scihhxcba"]; //不匹配
    /** .* 表示无限多个字符， 以 c[任意字符]a结尾 */
    regex = @"^.*c.a$";
    [self printerRegex:regex object:@"scihhx"];   //不匹配
    [self printerRegex:regex object:@"scihhxcba"]; //匹配
}

#pragma mark- ==== Funcation ====

/**
 * @abstract  输出比对结果
 * @param regex  正则表达式
 * @param object 对象
 */
+ (void)printerRegex:(NSString *)regex object:(id)object {
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSLog(@"表达式:%@  对象:%@  结果:%@", regex, object, [pre evaluateWithObject:object] ? @"匹配" : @"不匹配");
}



@end




